package com.dmyang.task;

import com.dmyang.vo.UserVo;
import com.dmyang.websocket.MyWebSocketHandler;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.SynchronousQueue;
/**
 * Created by brss on 2018/5/13.
 */


/**
 *
 * 类名称：TimerTask
 * 类描述：定时器任务
 * 创建人：geekfly
 * 创建时间：Aug 29, 2016 10:56:27 AM
 * @version  V1.0
 *
 */
@Component
public class TimerTask {

    @Scheduled(cron = "0/3 * * * * ?")//每隔3秒隔行一次
    public void test2() {
        Map<String, WebSocketSession> map= MyWebSocketHandler.userSocketSessionMap;
        ArrayBlockingQueue doctorQueue = MyWebSocketHandler.doctorQueue;
        ArrayBlockingQueue patientQueue = MyWebSocketHandler.patientQueue;
        Map<String, List<UserVo>> relation_doc = MyWebSocketHandler.relation_doc;
        Map<String, List<UserVo>> relation_pat = MyWebSocketHandler.relation_pat;
//        System.out.println("webSocketSession的长度为："+map.size());
        //往队列中加人
        for (String key : map.keySet()){
            String role = (String) map.get(key).getAttributes().get("role");
            String name = (String) map.get(key).getAttributes().get("name");
            String pk = (String) map.get(key).getAttributes().get("pk");

            if (role.equals("doctor")  && !(boolean) map.get(key).getAttributes().get("isBusy") ){
                //不繁忙的医生 才入队
                int treatNum = (int) map.get(key).getAttributes().get("treatNum");
                for (int i = 3 - treatNum ; i > 0 ; i-- ){
                    UserVo userVo = new UserVo();
                    userVo.setName(name);
                    userVo.setId(pk);
                    userVo.setType("kefu");
                    userVo.setAvatar("//res.layui.com/images/fly/avatar/00.jpg");
                    try {
                        doctorQueue.add(userVo);//同一个医生，往队列中加3-treatNum次
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
//                    修改医生状态
                    map.get(key).getAttributes().put("isBusy",true);
                    map.get(key).getAttributes().put("treatNum",treatNum);
                }
            }else if (role.equals("patient")  && !(boolean)map.get(key).getAttributes().get("isTreated") ){
                //等待的病人才入队
                boolean isTreated = (boolean) map.get(key).getAttributes().get("isTreated");
                if ( !isTreated ){
                    UserVo userVo = new UserVo();
                    userVo.setName(name);
                    userVo.setId(pk);
                    userVo.setType("kefu");
                    userVo.setAvatar("//res.layui.com/images/fly/avatar/00.jpg");
                    try {
                        patientQueue.add(userVo);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    //修改患者状态
                    map.get(key).getAttributes().put("isTreated",true);
                }
            }else {
                //System.out.println("未知角色,或已接待患者，或");
            }
        }//队列添加end
        //遍历队列
        //System.out.println("病人队列长度："+patientQueue.size());
        //System.out.println("医生队列长度："+doctorQueue.size());
        //最大可匹配数（病人数和医生数较小的那一项）
        int maxMatchNum = 0;
        if ( patientQueue.size() >= doctorQueue.size() )
            maxMatchNum = doctorQueue.size();
        else
            maxMatchNum = patientQueue.size();
        //System.out.println("最大可匹配数:"+maxMatchNum);

        //开始匹配
        for ( int i = 0 ; i < maxMatchNum ; i++ ){
            UserVo patient = (UserVo)patientQueue.poll();
            UserVo doctor = (UserVo)doctorQueue.poll();
            if ( relation_pat.get(patient.getId()) == null ){
                ArrayList<UserVo> userVoList = new ArrayList<>();
                relation_pat.put(patient.getId(),userVoList);
            }
            //relation_pat 是： 病人关系map：key：病人主键，v 医生实体
            relation_pat.get(patient.getId()).add(doctor);

            if ( relation_doc.get(doctor.getId()) == null ){
                ArrayList<UserVo> userVoList = new ArrayList<>();
                relation_doc.put(doctor.getId(),userVoList);
            }
            relation_doc.get(doctor.getId()).add(patient);
        }
        /*
        System.out.println("匹配完成，结果如下：");
        for (String k : relation_doc.keySet()){
            System.out.println(k+","+relation_doc.get(k));
        }

        for (String k : relation_pat.keySet()){
            System.out.println(k+","+relation_pat.get(k));
        }
        */


    }
}